package com.hengpeng.itfinoss.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageZipUtil
{

	private int image_width;

	private int image_height;

	public int getImage_height()
	{
		return image_height;
	}

	public void setImage_height(int image_height)
	{
		this.image_height = image_height;
	}

	public int getImage_width()
	{
		return image_width;
	}

	public void setImage_width(int image_width)
	{
		this.image_width = image_width;
	}

	/**
	 * 图片压缩处理 -- 等比压缩
	 * @param oldFile
	 * @param width
	 * @param height
	 * @param quality
	 * @param smallIcon
	 * @return
	 */
	public String zipImageFile(String oldFile, float quality, String smallIcon)
	{
		if (oldFile == null)
		{
			return null;
		}
		String newImage = null;
		try
		{
			/** 读取需要压缩的文件 */
			Image srcFile = ImageIO.read(new File(oldFile));

			int height = srcFile.getHeight(null);
			int width = srcFile.getWidth(null);

			// 等比处理
			// if (height > this.image_height)
			// {
			// this.image_width = this.image_height * width / height;
			// }
			// else if (width > this.image_width)
			// {
			// this.image_height = this.image_width * height / width;
			// }
			// else
			// {
			// return null;
			// }
			double rate1 = ((double) width) / (double) this.image_width + 0.1;
			double rate2 = ((double) height) / (double) this.image_height + 0.1;
			// 根据缩放比率大的进行缩放控制
			double rate = rate1 > rate2 ? rate1 : rate2;
			this.image_width = (int) (((double) width) / rate);
			this.image_height = (int) (((double) height) / rate);
			BufferedImage tag = new BufferedImage(this.image_width, this.image_height, BufferedImage.TYPE_INT_RGB);

			// 等比压缩
			// tag.getGraphics().drawImage(srcFile,0,0,this.image_width, this.image_height,, null);
			tag.getGraphics().drawImage(
					srcFile.getScaledInstance(this.image_width, this.image_height, Image.SCALE_SMOOTH), 0, 0, null);

			String filePrex = oldFile.substring(0, oldFile.indexOf('.'));

			newImage = filePrex + smallIcon + oldFile.substring(filePrex.length());

			FileOutputStream out = new FileOutputStream(newImage);

			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);

			jep.setQuality(quality, true);
			encoder.encode(tag, jep);
			out.close();

		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return newImage;
	}

	/**
	 * <pre>
	 *    写入文件
	 *  </pre>
	 * @param fileName
	 * @param is
	 * @return 文件名
	 */
	public String writeFile(String fileName, InputStream is)
	{
		if (fileName == null || fileName.trim().length() == 0)
		{
			return null;
		}
		try
		{

			FileOutputStream fos = new FileOutputStream(fileName);
			byte[] readBytes = new byte[512];
			int readed = 0;
			while ((readed = is.read(readBytes)) > 0)
			{
				fos.write(readBytes, 0, readed);
			}
			fos.close();
			is.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return fileName;
	}

}
